/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-12-16
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.business.printservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.mapper.WmsOutReceiptMapper;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.printservice.entity.WmsBusinessPrintDetail;
import org.jeecg.modules.business.printservice.entity.WmsBusinessPrintInfo;
import org.jeecg.modules.business.printservice.entity.vo.SaleOutPrintVO;
import org.jeecg.modules.business.printservice.service.IWmsBusinessPrintDetailService;
import org.jeecg.modules.business.printservice.service.IWmsBusinessPrintInfoService;
import org.jeecg.modules.business.printservice.service.IWmsGeneralPrintService;
import org.jeecg.modules.dock.entity.dto.request.SearchPrintInfoRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.SearchPrintInfoHeadResponseDTO;
import org.jeecg.modules.dock.entity.dto.response.SearchPrintInfoItemResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 通用打印业务服务实现
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-12-16
 * @description 类描述
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WmsGeneralPrintServiceImpl implements IWmsGeneralPrintService {

    private final IWmsOutReceiptService iWmsOutReceiptService;

    private final IWmsOutReceiptMaterialService iWmsOutReceiptMaterialService;

    private final IWmsBusinessPrintInfoService iWmsBusinessPrintInfoService;

    private final IWmsBusinessPrintDetailService iWmsBusinessPrintDetailService;

    private final SapDockingRepeaterService sapDockingRepeaterService;

    /**
     * 销售出库单打印
     *
     * @author DongPeng
     * @date 2023/12/16 13:48
     * @param vo 出库单id
     * @return
     */
    @Override
    @Synchronized
    @Transactional(rollbackFor = Exception.class)
    public Result<String> saleOutPrint(SaleOutPrintVO vo) {
        WmsOutReceipt wmsOutReceipt = iWmsOutReceiptService.getById(vo.getId());
        if (ObjectUtil.isEmpty(wmsOutReceipt)) {
            throw new JeecgBootException("没有查询到此销售出库单信息！");
        }
        WmsBusinessPrintInfo info = iWmsBusinessPrintInfoService.getOne(new LambdaQueryWrapper<WmsBusinessPrintInfo>().eq(WmsBusinessPrintInfo::getDocumentCode, wmsOutReceipt.getDocumentCode()).eq(WmsBusinessPrintInfo::getDocumentType, wmsOutReceipt.getDocumentType()));
        //先看打印记录中有没有此单据信息，有的话直接返，没有去调接口获取
        if (ObjectUtil.isEmpty(info)) {
            //获取交货单号
            String deliveryOrder = "";
            List<WmsOutReceiptMaterial> materials = iWmsOutReceiptMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptMaterial>().eq(WmsOutReceiptMaterial::getWmsOutReceiptId, wmsOutReceipt.getId()));
            if (ObjectUtil.isNotEmpty(materials)) {
                //构参
                deliveryOrder = materials.get(0).getOriginalSystemNumber();
                if (StrUtil.isEmpty(deliveryOrder)) {
                    throw new JeecgBootException("查询不到此销售出库单的交货单号！");
                }
                SearchPrintInfoRequestDTO searchVO = new SearchPrintInfoRequestDTO();
                searchVO.setUuid(IdUtil.fastUUID().replaceAll("-",""));
                searchVO.setDeliveryNote(deliveryOrder);
                //调用打印信息接口
                SearchPrintInfoHeadResponseDTO dto = sapDockingRepeaterService.saleOutboundPrintInfo(searchVO);
                if (ObjectUtil.isEmpty(dto)) {
                    throw new JeecgBootException("向sap系统获取打印信息失败，请稍后重试！");
                }
                //直接转换没有id赋值
                WmsBusinessPrintInfo newInfo = new WmsBusinessPrintInfo();
                //存记录
                newInfo = BeanUtil.toBean(dto, WmsBusinessPrintInfo.class);
                newInfo.setDocumentType(wmsOutReceipt.getDocumentType());
                newInfo.setDocumentCode(wmsOutReceipt.getDocumentCode());
                iWmsBusinessPrintInfoService.save(newInfo);
                String wmsBusinessPrintInfoId = newInfo.getId();
                List<SearchPrintInfoItemResponseDTO> responses = dto.getItemResponses();
                for (SearchPrintInfoItemResponseDTO response : responses) {
                    //存详细
                    WmsBusinessPrintDetail detail = new WmsBusinessPrintDetail();
                    detail = BeanUtil.toBean(response, WmsBusinessPrintDetail.class);
                    detail.setWmsBusinessPrintInfoId(wmsBusinessPrintInfoId);
                    //查数量  2024-01-10  sap增加数量字段，所以不需要自己查数量
//                    BigDecimal quantity = BigDecimal.ZERO;
//                    for (WmsOutReceiptMaterial material : materials) {
//                        if (material.getMaterialCode().equals(detail.getMaterialCode())
//                               // || material.getMaterialCode().substring(8).equals(detail.getMaterialCode())
//                        ) {
//                            quantity = material.getOutboundQuantity();
//                        }
//                    }
//                    detail.setQuantity(quantity);
                    iWmsBusinessPrintDetailService.save(detail);
                }
            }
        }
        return Result.OK();
    }
}
